package com.lp.controller;

import com.lp.userException.CustomException;
import com.lp.po.Comment;
import com.lp.po.PagingVO;
import com.lp.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/Comment")
public class AdminCommentController {
    @Resource
    private CommentService commentService;
    /***************************景点评论管理设置****************************************/
    @RequestMapping("/showComment")
    public String showComment(Model model,Integer page) throws Exception {
        List<Comment> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int commentNumber=commentService.getCommentCount();
        //设置总页数
        pagingVO.setTotalCount(this.commentService.getCommentCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = commentService.findComment(1);
        } else {
            pagingVO.setToPageNo(page);
            list = commentService.findComment(page);
        }
        model.addAttribute("num",commentNumber);
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("comment",list);
        return "/admin/showComment";
    }

    @RequestMapping(value = "/editComment",method = RequestMethod.GET)
    public String editSceneryList(Model model,Integer id) throws Exception {
        if (id == null) {
            return "redirect:/admin/Comment/showComment";
        }
        Comment comment = commentService.findById(id);
        if (comment == null) {
            throw new CustomException("没有该记录");
        }
        model.addAttribute("comment", comment);
        return "/admin/editComment";
    }

    @RequestMapping(value = "/doEditComment",method = RequestMethod.POST)
    public String doEditComment(Comment comment, Integer id, String zhanshi) throws Exception {
        commentService.updataComment(zhanshi,id);
        return "redirect:/admin/Comment/showComment";
    }



    @RequestMapping(value = "/removeComment")
    public String removeSceneryList(Integer id) throws Exception {
        commentService.delectcomment(id);
        return "redirect:/admin/Comment/showComment";
    }
}
